package com.bsj.sipgateway.util;

import com.alibaba.fastjson.JSON;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.util.DateUtils;
import com.bsj.sipgateway.common.def.RecordFile;
import com.bsj.sipgateway.common.def.RecordList;
import org.apache.poi.hssf.record.Record;
import redis.clients.bsj.JedisClusterBSJ;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 设备录像存储工具类
 * @Author zhaoyang
 * @Date 2024/7/8 14:11
 */
public class RecordInfoUtil {


    // 保存录像列表到redis
    public static void saveRecordInfoToRds(String channelId, RecordList recordList, JedisClusterBSJ jedisClusterBSJ, int saveSecond) {
        if (jedisClusterBSJ == null || recordList == null
                || recordList.getListFile() == null || recordList.getListFile().size() == 0){
            return;
        }
        List<RecordFile> listFile = recordList.getListFile();
        RecordFile recordFile = listFile.get(0);
        // 前端是按照每一天查询的，用时间作为key的一部分即可
        long endtime = recordFile.getEndtime();
        String dateToStr = DateUtils.dateToStr(DateUtils.TIME_STANDARD_FORMAT_YMD, new Date(endtime));
        String key = RedisConstant.RECORD_INFO + channelId + dateToStr;
        jedisClusterBSJ.setex(key, saveSecond, JSON.toJSONString(recordList));
    }

    // startTime yyyy-MM-dd HH:mm:ss
    public static RecordList findRecordInfo(String channelId, String startTime, String endTime, JedisClusterBSJ jedisClusterBSJ) {
        if (channelId == null || startTime == null || endTime == null || jedisClusterBSJ == null) {
            return null;
        }
        // 先查询再过滤
        // 简单处理，按空格分开取第一个替换"-"
        String startTimeStr = startTime.split(" ")[0];
        String dateToStr = startTimeStr.replaceAll("-", "");
        String key = RedisConstant.RECORD_INFO + channelId + dateToStr;
        String value = jedisClusterBSJ.get(key);
        if (value == null) {
            return null;
        }
        return JSON.parseObject(value, RecordList.class);

    }


}
